<template>
  <el-dialog
    width="500px"
    custom-class="customWidth"
    center
    :before-close="closeDialog"
    :title="myTitle"
    :close-on-click-modal="false"
    :visible.sync="DialogFormVisible"
  >
    <el-form
      :model="deptForm"
      :rules="deptRule"
      ref="deptForm"
      @keyup.enter.native="FormSubmit()"
      label-width="80px"
    >
      <el-form-item label="上级部门" prop="parentDepartmentGuid">
        <el-col :span="15">
          <el-select
            id="deptSelect"
            v-model="deptForm.parentDepartmentName"
            clearable
            placeholder="请选择"
            style="width: 100%"
            size="mini"
            @change="currentSel"
          >
            <el-option
              v-for="item in parentDeptForm"
              :key="item.departmentGuid"
              :label="item.departmentName"
              :value="item.departmentGuid"
            ></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="部门名称" prop="departmentName">
        <el-col :span="15">
          <el-input
            v-model="deptForm.departmentName"
            placeholder="部门名称"
            size="mini"
            maxlength="20"
          ></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="负责人" prop="departmentDirector">
        <el-col :span="15">
          <el-input
            v-model="deptForm.departmentDirector"
            placeholder="负责人"
            size="mini"
            maxlength="20"
          ></el-input>
        </el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible()">取消</el-button>
      <el-button type="primary" @click="FormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      DialogFormVisible: false,
      myTitle: "",
      deptForm: {
        departmentDirector: "",
        departmentGuid: "",
        departmentName: "",
        isDelete: 0,
        parentDepartmentGuid: "",
        parentDepartmentName: "",
        pkDepartmentId: 0,
        syncState: 0,
        unitGuid: "",
        userGuid: "",
      },
      parentDeptForm: [],
      deptRule: {
        departmentName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.unitGuid = this.$store.state.user.changeschoolid;
    this.userGuid = sessionStorage.getItem("administrator");
  },
  methods: {
    currentSel(selVal) {
      this.deptForm.parentDepartmentGuid = selVal;
    },
    getParentDeptList(mType) {
      let mUrl, mDepartmentGuid;
      if (mType === 1) {
        mUrl = this.$http.adornUrl("/department/query-department-info");
        mDepartmentGuid = "";
      } else {
        mUrl = this.$http.adornUrl("/department/department-not-children");
        mDepartmentGuid = this.deptForm.parentDepartmentGuid;
      }
      this.$http({
        url: mUrl,
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            agentGuid: this.userGuid,
            departmentGuid: mDepartmentGuid,
            unitGuid: this.unitGuid,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        // console.log(this.userGuid,this.deptForm.parentDepartmentGuid,this.unitGuid);
        if (data.messageCode == 200) {
          this.parentDeptForm = data.messageContent;
          // this.deptForm.parentDepartmentName = data.messageContent.parentDepartmentName
        } else {
          // this.$message.error(data.errorMessage)
        }
      });
    },
    getParentDeptInfo() {
      // 获取上级部门信息
      this.$http({
        url: this.$http.adornUrl("/department/select-department-detail"),
        method: "post",
        data: this.$http.adornData({
          messageContent: this.deptForm.parentDepartmentGuid,
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.deptForm.parentDepartmentName =
            data.messageContent.departmentName;
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    addinit(parentDeptGuid, parentDeptName) {
      this.DialogFormVisible = true;
      this.myTitle = "新增部门";
      this.deptForm.parentDepartmentGuid = parentDeptGuid;
      this.deptForm.parentDepartmentName = parentDeptName;
      // // console.log(this.deptForm.parentDepartmentGuid)
      this.getParentDeptList(1);
      // if (parentDeptGuid !== "") {
      //   this.getParentDeptInfo();
      // }
    },
    editinit(deptGuid, parentDeptGuid) {
      if (deptGuid === "") {
        return;
      }
      this.DialogFormVisible = true;
      this.myTitle = "修改部门";
      this.deptForm.departmentGuid = deptGuid;
      this.deptForm.parentDepartmentGuid = parentDeptGuid;
      // 获取需要修改的部门
      this.$http({
        url: this.$http.adornUrl("/department/select-department-detail"),
        method: "post",
        data: this.$http.adornData({
          messageContent: this.deptForm.departmentGuid,
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          // console.log(data.messageContent);
          this.deptForm.departmentName = data.messageContent.departmentName;
          this.deptForm.departmentDirector =
            data.messageContent.departmentDirector;
        } else {
          this.$message.error(data.errorMessage);
          this.searchInfo1 = {};
        }
      });
      if (parentDeptGuid !== "") {
        this.getParentDeptList(2);
        this.getParentDeptInfo();
      } else {
        deptSelect.value = "";
      }
    },
    closeDialog(done) {
      this.$refs["deptForm"].resetFields();
      this.parentDeptForm = [];
      done();
    },
    FormSubmit() {
      // console.log(this.userGuid,",",this.deptForm.departmentDirector,",",this.deptForm.departmentGuid,",",this.deptForm.departmentName,",",this.deptForm.parentDepartmentGuid,",",this.unitGuid)
      if (this.myTitle == "新增部门") {
        this.$http({
          url: this.$http.adornUrl("/department/info-department-add"),
          method: "post",
          data: this.$http.adornData({
            messageContent: {
              agentGuid: this.userGuid,
              departmentDirector: this.deptForm.departmentDirector,
              departmentGuid: this.deptForm.departmentGuid,
              departmentName: this.deptForm.departmentName,
              parentDepartmentGuid: this.deptForm.parentDepartmentGuid,
              unitGuid: this.unitGuid,
            },
            messageType: 0,
          }),
        }).then(({ data }) => {
          if (data.messageCode == 200) {
            this.$message({
              message: "新增成功",
              type: "success",
              duration: 1500,
              // onClose: () => {
              //   this.DialogFormVisible = false
              //   this.$emit('refreshDataList')
              // }
            });
            this.visible();
          } else {
            this.$message.error(data.errorMessage);
            return;
          }
        });
      } else {
        this.$http({
          url: this.$http.adornUrl("/department/info-department-update"),
          method: "post",
          data: this.$http.adornData({
            messageContent: {
              agentGuid: this.userGuid,
              departmentDirector: this.deptForm.departmentDirector,
              departmentGuid: this.deptForm.departmentGuid,
              departmentName: this.deptForm.departmentName,
              parentDepartmentGuid: this.deptForm.parentDepartmentGuid,
              unitGuid: this.unitGuid,
            },
            messageType: 0,
          }),
        }).then(({ data }) => {
          if (data.messageCode == 200) {
            this.$message({
              message: "修改成功",
              type: "success",
              duration: 1500,
              // onClose: () => {
              //   this.DialogFormVisible = false
              //   this.$emit('refreshDataList')
              // }
            });
            this.visible();
          } else {
            this.$message.error(data.errorMessage);
            return;
          }
        });
      }
    },
    visible() {
      this.DialogFormVisible = false;
      this.$refs["deptForm"].resetFields();
      this.parentDeptForm = [];
      this.$emit("refreshDataList");
    },
  },
};
</script>

<style lang="scss" scoped>
</style>